package command.commandRunnable;

import dao.Factory;
import database.UserData;
import entity.Client;
import logger.LoggerManager;
import parsers.ClientJsonParser;
import parsers.UserDataParser;
import resurses.LoggerMessage;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by alexander.makalov on 24.10.2014.
 */
public class CommandJSonParser implements Runnable {

    public String getJSONFilePath() {
        return JSONFilePath;
    }

    public void setJSONFilePath(String JSONFilePath) {
        this.JSONFilePath = JSONFilePath;
    }

    @Override
    public void run() {
        if(UserData.checkData()&& JSONFilePath != null){
            startParsing();
        }else {
            LoggerManager.getLogger().Log(LoggerMessage.UPDATE_USER_INFO, LoggerManager.LogLevel.INFO);
            UserDataParser userDataParser = new UserDataParser();
            userDataParser.startParsing();
            startParsing();
        }
    }

    private void startParsing(){
        ClientJsonParser jsonParser = new ClientJsonParser(JSONFilePath);
        jsonParser.startParsing();
        Factory.getInstance().getClientDAO().saveAllClients(jsonParser.getClientsList());
    }

    private String JSONFilePath;
}
